# -*- coding: utf-8 -*-
"""
装饰器实现工厂模式
"""
import logging

import py_global

logger = logging.getLogger(__name__)

# 注册表
_registry_dict = dict()
registry_name = 'registry_list'


def register(cls):
    if not hasattr(cls, registry_name):
        logger.error(f'{cls.__name__} not has {registry_name}')
        return
    for registry in getattr(cls, registry_name):
        _registry_dict[registry] = cls
        pass


def get_registry_class(registry):
    return _registry_dict.get(registry, Register)


class Register(object):
    # registry_list = ['Register']

    def register_method(self):
        logger.info(f'class:{self.__class__.__name__}')


@register
class Register0(object):
    registry_list = ['Register0']

    def register_method(self):
        logger.info(f'class:{self.__class__.__name__}')


@register
class Register1(object):
    registry_list = ['Register1']

    def register_method(self):
        logger.info(f'class:{self.__class__.__name__}')


@register
class Registers(object):
    # 允许多个
    registry_list = ['Register', 'Registers']

    def register_method(self):
        logger.info(f'class:{self.__class__.__name__}')


if __name__ == '__main__':
    registry_list = ['Register0', 'Register1', 'Register', 'Registers']
    for registry_name in registry_list:
        register_class = get_registry_class(registry_name)
        register_class().register_method()
        pass

    pass
